package demo.p1;

import demo.Api;
import demo.BestUse1;
import demo.api.ServerApi;
import demo.api.ServerApi1;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("getServer")
@RefreshScope
public class getServer {

    @Value("${nacos.config.name}")
    private String configName;

    @Resource
    RestTemplate restTemplate;

    @RequestMapping("name")
    String getNma() {
        return configName;
    }

    @Autowired
    Api api;

    @Autowired
    BestUse1 bestUse1;

    @Autowired
    ServerApi1 serverApi1;

    @RequestMapping("rr1")
    String rr1() {
        String s = serverApi1.productId("hhhh");
        System.out.println(s);
        return s;
    }


    @RequestMapping("rr0")
    String rr0() {
        System.out.println(bestUse1.productId3("xxxxxxxx"));
        String s = bestUse1.productId("wowowo");
        System.out.println(s);
        System.out.println(bestUse1.productId2(List.of("1", "2", "3", "4"), Set.of(1, 2, 3, 4)));
        return s;
    }

    @RequestMapping("r0")
    String r0() {
        System.out.println(api.productId("ro"));
        System.out.println(api.productId2(List.of("1", "2", "3", "4"), Set.of(1, 2, 3, 4)));
        return api.productId("ro");
    }


    @RequestMapping("r1")
    void get() {
        System.out.println(restTemplate);
        System.out.println("get");
        String url = "http://order-server/order/self/self";
        String forObject = restTemplate.getForObject(url, String.class);
        System.out.println(forObject);
    }

    @RequestMapping("/self")
    String getSelf() {
        System.out.println(restTemplate.getClass());
        //使用了LoadBalanced 他就无法找到这个服务名
        String url = "http://hadage/self1";
        String forObject = restTemplate.getForObject(url, String.class);
        return "111";//不是这个
    }

    @RequestMapping("/self1")
    String getSelf1(HttpServletRequest request, @RequestParam("hh") String hh, String www,String p1) {

        System.out.println(request.getHeader("h"));
        System.out.println(request.getHeader("del"));
        System.out.println(request.getHeader("ww"));

        System.out.println(request.getRequestURL() + "?" + request.getQueryString());

        System.out.println(hh);
        System.out.println(www);

        System.out.println("selffffff");

        System.out.println();
        return "1112";
    }
}
